| Conditions | 1 |
| Paths | 3 |
| Total Lines | 171 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 6 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | /* global API */ |
||
| 37 | function ($scope, $timeout, $location, $rootScope, StepsService, notify, HttpsTest) { |
||
| 38 | $scope.settings = { |
||
| 39 | nextcloud_host: '', |
||
| 40 | nextcloud_username: '', |
||
| 41 | nextcloud_password: '', |
||
| 42 | ignoreProtocol: true, |
||
| 43 | ignoreSubdomain: true, |
||
| 44 | ignorePath: true, |
||
| 45 | generatedPasswordLength: 12, |
||
| 46 | remember_password: true, |
||
| 47 | vault_password: '', |
||
| 48 | refreshTime: 60, |
||
| 49 | default_vault: {}, |
||
| 50 | master_password: '', |
||
| 51 | master_password_repeat: '', |
||
| 52 | enableAutoFill: true, |
||
| 53 | enablePasswordPickerr: true, |
||
| 54 | enableAutoSubmit: false, |
||
| 55 | debug: false, |
||
| 56 | accounts: [] |
||
| 57 | }; |
||
| 58 | $scope.vaults = []; |
||
| 59 | |||
| 60 | $rootScope.$broadcast('hideHeader'); |
||
| 61 | $rootScope.setup = true; |
||
| 62 | $scope.gogo = function (to) { |
||
| 63 | StepsService.steps().goTo(to); |
||
| 64 | }; |
||
| 65 | notify.config({ |
||
| 66 | 'position': 'left', |
||
| 67 | 'duration': 2500 |
||
| 68 | }); |
||
| 69 | |||
| 70 | $scope.check = { |
||
| 71 | server: function (callback) { |
||
| 72 | if(!$scope.settings.nextcloud_host || !$scope.settings.nextcloud_username || !$scope.settings.nextcloud_password){ |
||
| 73 | $scope.errors.push(API.i18n.getMessage('invalid_server_settings')); |
||
| 74 | callback(false); |
||
| 75 | return; |
||
| 76 | } |
||
| 77 | $scope.settings.nextcloud_host = $scope.settings.nextcloud_host.replace(/\/$/, ""); |
||
| 78 | PAPI.host = $scope.settings.nextcloud_host; |
||
| 79 | PAPI.username = $scope.settings.nextcloud_username; |
||
| 80 | PAPI.password = $scope.settings.nextcloud_password; |
||
| 81 | PAPI.getVaults(function (vaults) { |
||
| 82 | if (vaults.hasOwnProperty('error')) { |
||
| 83 | var errors = API.i18n.getMessage('invalid_response_from_server', [vaults.result.status, vaults.result.statusText]); |
||
| 84 | $scope.errors.push(errors); |
||
| 85 | notify(errors); |
||
| 86 | callback(false); |
||
| 87 | } |
||
| 88 | else { |
||
| 89 | $scope.vaults = vaults; |
||
| 90 | callback(true); |
||
| 91 | } |
||
| 92 | $scope.$apply(); |
||
| 93 | }); |
||
| 94 | }, |
||
| 95 | vault: function (callback) { |
||
| 96 | try { |
||
| 97 | PAPI.decryptString($scope.settings.default_vault.challenge_password, $scope.settings.vault_password); |
||
| 98 | callback(true); |
||
| 99 | } |
||
| 100 | catch (e) { |
||
| 101 | $scope.errors.push(); |
||
| 102 | notify(API.i18n.getMessage('invalid_vault_password')); |
||
| 103 | callback(false); |
||
| 104 | } |
||
| 105 | }, |
||
| 106 | master: function (callback) { |
||
| 107 | if($scope.settings.master_password !== $scope.settings.master_password_repeat){ |
||
| 108 | notify(API.i18n.getMessage('no_password_match')); |
||
| 109 | callback(false); |
||
| 110 | return; |
||
| 111 | } |
||
| 112 | |||
| 113 | if ($scope.settings.master_password.trim() !== '') { |
||
| 114 | callback(true); |
||
| 115 | } else { |
||
| 116 | notify(API.i18n.getMessage('empty_master_key')); |
||
| 117 | callback(false); |
||
| 118 | } |
||
| 119 | } |
||
| 120 | }; |
||
| 121 | $scope.saving = false; |
||
| 122 | $scope.next = function () { |
||
| 123 | $scope.saving = true; |
||
| 124 | $scope.errors = []; |
||
| 125 | $timeout(function () { |
||
| 126 | var step = StepsService.getCurrent().name; |
||
| 127 | var check = $scope.check[step]; |
||
| 128 | if (typeof check === "function") { |
||
| 129 | check(function (result) { |
||
| 130 | $scope.saving = false; |
||
| 131 | if (result) { |
||
| 132 | $scope.errors = []; |
||
| 133 | $scope.$apply(); |
||
| 134 | StepsService.steps().next(); |
||
| 135 | } |
||
| 136 | $timeout(function () { |
||
| 137 | $scope.errors = []; |
||
| 138 | $scope.$apply(); |
||
| 139 | }, 5000); |
||
| 140 | }); |
||
| 141 | } |
||
| 142 | else { |
||
| 143 | $scope.saving = false; |
||
| 144 | StepsService.steps().next(); |
||
| 145 | } |
||
| 146 | }, 10); |
||
| 147 | }; |
||
| 148 | |||
| 149 | var handleCheck = function (resultUrl) { |
||
| 150 | $scope.settings.nextcloud_host = resultUrl; |
||
| 151 | }; |
||
| 152 | |||
| 153 | $scope.isHTTP = function (url) { |
||
| 154 | return HttpsTest.isHTTP(url); |
||
| 155 | }; |
||
| 156 | |||
| 157 | $scope.checkHost = function () { |
||
| 158 | HttpsTest.test($scope.settings.nextcloud_host).then(handleCheck, handleCheck); |
||
| 159 | }; |
||
| 160 | |||
| 161 | $scope.finished = function () { |
||
| 162 | var settings = angular.copy($scope.settings); |
||
| 163 | var master_password = settings.master_password; |
||
| 164 | var master_password_remember = settings.master_password_remember; |
||
| 165 | var account = { |
||
| 166 | nextcloud_host: settings.nextcloud_host, |
||
| 167 | nextcloud_username: settings.nextcloud_username, |
||
| 168 | nextcloud_password: settings.nextcloud_password, |
||
| 169 | vault: settings.default_vault, |
||
| 170 | vault_password: settings.vault_password |
||
| 171 | }; |
||
| 172 | settings.accounts.push(account); |
||
| 173 | delete settings.master_password; |
||
| 174 | delete settings.master_password_remember; |
||
| 175 | delete settings.nextcloud_host; |
||
| 176 | delete settings.nextcloud_username; |
||
| 177 | delete settings.nextcloud_password; |
||
| 178 | delete settings.vault_password; |
||
| 179 | delete settings.master_password_repeat; |
||
| 180 | delete settings.default_vault; |
||
| 181 | |||
| 182 | $scope.saving = true; |
||
| 183 | |||
| 184 | API.runtime.sendMessage(API.runtime.id, { |
||
| 185 | method: "setMasterPassword", |
||
| 186 | args: {password: master_password, savePassword: master_password_remember} |
||
| 187 | }) |
||
| 188 | .then(function () { |
||
| 189 | API.runtime.sendMessage(API.runtime.id, { |
||
| 190 | method: "saveSettings", |
||
| 191 | args: settings |
||
| 192 | }).then(function () { |
||
| 193 | setTimeout(function () { |
||
| 194 | $rootScope.setup = false; |
||
| 195 | $rootScope.$broadcast('showHeader'); |
||
| 196 | window.location = '#!/'; |
||
| 197 | API.runtime.sendMessage(API.runtime.id, { |
||
| 198 | method: "closeSetupTab" |
||
| 199 | }); |
||
| 200 | $scope.saving = false; |
||
| 201 | }, 750); |
||
| 202 | }); |
||
| 203 | }); |
||
| 204 | |||
| 205 | |||
| 206 | }; |
||
| 207 | }]); |
||
| 208 | }()); |
||
| 210 |